<!-- #INCLUDE FILE="../includes/ValidateDate.asp" -->
<%
Dim CDEmploymentLength
Dim i
Dim strArrLossOther
Dim strArrLossNature
dim sSaveMessage
dim strDateType

lstRFD					= Trim(Request.Form("lstRFD"))
lstRFDIndex				= Trim(Request.Form("txtlstRFD"))
nEmployeeNo				= Trim(Request.Form("txtEmployeeNo"))
dtCDEmployment			= Trim(Request.Form("txtCDEmployment"))
strDateType				= "commencement"
nTaxYear				= Trim(Request.Form("txtTaxYear"))
lstYN					= Trim(Request.Form("lstYN"))
lstYNIndex				= Trim(Request.Form("txtlstYN"))
nTaxPayersAnnualIncome	= Trim(Request.Form("txtTaxPayersAnnualIncome"))
nGrossInc				= Trim(Request.Form("txtGrossInc"))
nEstAdmExp				= Trim(Request.Form("txtEstAdmExp"))
nTaxPayable				= Trim(Request.Form("txtTaxPayable"))
nLossAss				= Trim(Request.Form("txtLossAss"))
nFixedPayMonth			= Trim(Request.Form("txtFixedPayMonth"))
nLossNature				= Request.Form("txtLossNature")
nLossAmount				= Request.Form("txtLossAmount")

if sErrorMessage = "" then
	sErrorMessage = ValOtherLosses()
end if

if sErrorMessage = "" then
	sErrorMessage = ValOtherLossNatures()
end if

if sErrorMessage = "" then
	sErrorMessage = checkValues()
end if

if sErrorMessage = "" then
	sSaveMessage = SaveRequest()
end if

if instr(UCASE(sSaveMessage), "SUCCESS") AND sErrorMessage = "" then
	sErrorMessage = SaveOtherLosses(sSaveMessage)
end if

if instr(UCASE(sSaveMessage), "SUCCESS") and sErrorMessage = "" then
	Response.redirect "../directive/directive.asp"	
end if



'--------------------------------------------------------- set up the Other Losses array ----------------------------------
'Spec change: As per telephonic conversation with Phil Trollip dd 27/09/2001
'Public Function ValOtherLosses()

'	dim sFieldName
'	dim sNatureName

'	i = 0 

'	for i = i to ubound(ArrLossOther)
		
'		sFieldName = "txtLossOther" & i
'		sNatureName = "txtLossNature" & i
		
'		ArrLossOther(i) = Trim(Request.Form("" & sFieldName & ""))
'		ArrLossNature(i) = Trim(Request.Form("" & sNatureName & ""))
		
'		if ArrLossOther(i) = "," then
'			ArrLossOther(i) = ""
'		end if
		
'		if ArrLossNature(i) <> "" and ArrLossOther(i) = "" then
'			ValOtherLosses = "Please specify the corresponding amounts for all other loss nature fields completed"
'		end if
'	next
	
'End Function
'--------------------------------------------------------------------------------------------------------------------------
Public Function ValOtherLosses()

if nLossAmount <> "" and nLossNature = "" then
	ValOtherLosses = "Please specify the corresponding nature field for the other amount field completed"
End if

End Function



Public Function ValOtherLossNatures()

if nLossNature <> "" and nLossAmount = "" then
	ValOtherLosses = "Please specify the corresponding amount for the other loss nature field completed"
End if

End Function



'--------------------------------------------------------- set up the Nature of other Losses array ------------------------
'Public Function ValOtherLossNatures()

'	dim NatureLoop
'	dim NatureSubLoop
'	dim sMainValue
'	dim sSubValue
'	dim sFieldName

'	for NatureLoop = NatureLoop to ubound(ArrLossNature)
		
'		sFieldName = "txtLossNature" & NatureLoop
		
		'Response.Write "NatureLoop is " & NatureLoop & "<br>"
		
'		ArrLossNature(NatureLoop) = Trim(Request.Form("" & sFieldName & ""))
		
		'----------------------------------------------- check for duplicate other losses -------------------------------------
		'Spec change: As per telephonic conversation with Phil Trollip dd 27/09/2001
		'NatureSubLoop = 0
		
		'for NatureSubLoop = NatureSubLoop to ubound(ArrLossNature)
		
		'	sMainValue = ArrLossNature(NatureLoop)
		'	sSubValue = ArrLossNature(NatureSubLoop)
		
		'	if NatureLoop <> NatureSubLoop then
		'		if sMainValue = sSubValue and sMainValue <> "" then
		'			ValOtherLossNatures = "Please ensure that no duplicate other loss natures are specified."
		'		exit for
		'		end if
		'	end if	
		
		'next
		'----------------------------------------------------------------------------------------------------------------------
		
		'if ArrLossNature(NatureLoop) = "," then
		'	ArrLossNature(NatureLoop) = ""
		'end if
		
		'if ArrLossOther(NatureLoop) <> "" AND ArrLossNature(NatureLoop) = "" then
		'	ValOtherLossNatures = "Please specify all the corresponding ""Nature of other loss"" fields for every other loss specified."
		'end if
		
		'Response.Write "" & i & " - " & ArrLossNature(i) & "<br>"
	'next
	
'End Function	
'--------------------------------------------------------------------------------------------------------------------------





'---------------------------------------------------- some validation ------------------------------------------------------
Public Function checkValues()

	Dim sErrorMessage
	Dim sTempErrorMsg
	Dim aFocusFields()
	Dim strErrMsg
	Dim strDecimal

	Redim aFocusFields(7)
	
	sErrorMessage = ""
	
	If lstRFD = "" Then
		checkValues = checkValues & "Please specify a reason for the directive." & "<br>"
		aFocusFields(1) = "lstRFD"
	End If
	
	if nIncomeTaxReferenceNo <> "" then
		dim sRequestFor 'individuals or companies
	
		Select case lstRFD
			Case "14"
				sRequestFor = "Individual"
			Case else
				sRequestFor = "Company"
		end select
	
		if nIncomeTaxReferenceNo <> "" AND len(nIncomeTaxReferenceNo) < 10 then
			nIncomeTaxReferenceNo = "0" & cstr(nIncomeTaxReferenceNo)
		end if
	
		if sRequestFor = "Individual" AND NOT left(nIncomeTaxReferenceNo, 1) < 4 then
			checkValues = checkValues & "If the reason for the request is ""Paragraph 11, 4th Schedule"", only individual income tax numbers are allowed. These numbers start with 0 / 1 / 2 / 3)." & "<br>"
			'aFocusFields(2) = "lstRFD"
		end if
	end if
	
	if nTaxYear = "" then
		checkValues = checkValues & "Please specify the tax year" & "<br>"
		aFocusFields(3) = "txtTaxYear"
	Else
		sTempErrorMsg = TaxYear(nTaxYear)
		If Len(sTempErrorMsg) > 0 Then
			checkValues = checkValues & sTempErrorMsg
			aFocusFields(3) = "txtTaxYear"
		End If
	end if
	
	If dtCDEmployment <> "" then
		strErrMsg = ValidateDate(dtCDEmployment, strDateType)
		If strErrMsg <> "" Then
			checkvalues =  checkvalues & strErrMsg & "<br>"
			aFocusFields(4) = "txtCDEmployment"
		End If
	End if
	
	If Len(nTaxYear) < 4 Then
		checkValues = checkValues & "Please specify the tax year as YYYY." & "<br>"
		aFocusFields(3) = "txtTaxYear"
	End If 
	
	If nTaxPayersAnnualIncome = "" Then
		checkValues = checkValues & "Please specify the taxpayer's annual income." & "<br>"
		aFocusFields(5) = "txtTaxPayersAnnualIncome"
	End If
	
	If nGrossInc = "" Then
		checkValues = checkValues & "Please specify the taxpayer's gross income." & "<br>"
		aFocusFields(6) = "txtGrossInc"
	ElseIf nGrossInc <> "" Then
		If InStr(nGrossInc, ".") <> 0 Then
		 strDecimal = Right(nGrossInc, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			checkValues = checkValues & "Please capture the Estimated Gross Income in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	If nEstAdmExp <> "" Then
		If InStr(nEstAdmExp, ".") <> 0 Then
		 strDecimal = Right(nEstAdmExp, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			checkValues = checkValues & "Please capture the Estimated Admissable Expenditure in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	If nTaxPayable <> "" Then
		If InStr(nTaxPayable, ".") <> 0 Then
		 strDecimal = Right(nTaxPayable, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			checkValues = checkValues & "Please capture the Tax Payable amount in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	If nLossAss <> "" Then
		If InStr(nLossAss, ".") <> 0 Then
		 strDecimal = Right(nLossAss, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			checkValues = checkValues & "Please capture the Assessed Loss in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	If nFixedPayMonth <> "" Then
		If InStr(nFixedPayMonth, ".") <> 0 Then
		 strDecimal = Right(nFixedPayMonth, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			checkValues = checkValues & "Please capture the Fixed Amount in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	If nLossAmount <> "" Then
		If InStr(nLossAmount, ".") <> 0 Then
		 strDecimal = Right(nLossAmount, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			checkValues = checkValues & "Please capture the Other Loss amount in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	
	'for i = i to ubound(ArrLossOther)
		'if ArrLossOther(i) <> "" AND ArrLossNature(i) = "" then
		If nLossNature <> "" And nLossAmount = "" Then
			checkValues = checkValues & "Please specify all the corresponding ""Nature of other loss"" fields for every other loss specified." & "<br>"
			aFocusFields(7) = "txtLossNature"
		end if
	'next
	sFocusSet = GetFocusField(aFocusFields)
End function

'---------------------------------------------------------------------------------------------------------------------------



'------------------------------------------------------ Validate the tax year ----------------------------------------------

Public Function TaxYear(nTaxYear)

	Dim dtCurrentDate
	Dim dtCurrentYear
	Dim dtNextYear
	Dim dtCurrentMonth
	Dim dtTaxYear
	Dim dtNow

	dtNow = now

	dtCurrentMonth = Month(dtNow)'9
	dtCurrentYear = Year(dtNow)'2001

	If cdbl(dtCurrentMonth) = 1 OR cdbl(dtCurrentMonth) = 1 Then 'if the month is January or February, the tax year is the current year.
		dtTaxYear = cdbl(dtCurrentYear)
	Else
		dtTaxYear = cdbl(dtCurrentYear + 1) '2002
	End If

	If cdbl(nTaxYear) > cdbl(dtTaxYear) Then
		TaxYear = "" & nTaxYear & " is not a valid tax year. The tax year can only be " & dtTaxYear & " or earlier."
	End If

End Function 'TaxYear
'--------------------------------------------------------------------------------------------------------------------------	




'------------------------------------------------------ Save IRP3C request details ----------------------------------------

Public Function SaveRequest()

Dim dllSaveIRP3C
dim sSaveIRP3C

Dim dllDateConvert
Dim nReqNo

	Set dllDateConvert = Server.CreateObject("KeyTax.clsDateConvert")

	if dtBirthDate <> "" then
		dtBirthDate = dllDateConvert.StringDateToLongDate(dtBirthDate)
	End if
	

	if dtCDEmployment <> "" then
		dtCDEmployment = dllDateConvert.StringDateToLongDate(dtCDEmployment)
	End if
	
	Set dllDateConvert = nothing

	set dllSaveIRP3C = Server.CreateObject("KeyTax.clsLoadIRP3C")
	
	sSaveIRP3C = dllSaveIRP3C.SaveIRP3C(session("KeyTaxUser"), sNoTaxRefNoRsn, _
										sNoTaxRefNoText, _
										nIncomeTaxReferenceNo, _
										nRSAID, sOtherID, sSurname, sInitials, sFirstNames, _
										dtBirthDate, _
										sPost1, sPost2, sPost3, sPost4, sPostCode, _
										sHome1, sHome2, sHome3, sHome4, sHomeCode, _
										nTaxYear, lstRFD, sEmailTo, nEmpID, _
										nEmployeeNo, _
										nTaxPayersAnnualIncome, _
										nGrossInc, _
										dtCDEmployment, _
										nLossAss, _
										lstYN, _
										nEstAdmExp, _
										nTaxPayable, _
										nFixedPayMonth, _
										cstr(nReqNo))
		
	session("SaveMessage") = sSaveIRP3C
	
	'clean up
	set dllSaveIRP3C = nothing
	
	SaveRequest = sSaveIRP3C
	
End Function
'--------------------------------------------------------------------------------------------------------------------------	




'------------------------------------------------------ Save other losses ------------------------------------------------
Public Function SaveOtherLosses(sSaveMessage)
		
		
	'-------------------------------------------------------------------
	'get the request number from the save message
			
	dim nLength
	dim sMessage
	dim nReqNo
				
	nLength = instr(UCASE(sSaveMessage), "SUCCESS")
			
	sMessage = left(sSaveMessage, nLength - 1)
		
	nReqNo = mid(sMessage, 8)
			
	nReqNo = Replace(nReqNo, " ", "")
			
	'-------------------------------------------------------------------
	'Spec change: As per telephonic conversation with Phil Trollip dd 27/09/2001		
	'------------------------------------------------------------------	
	'dim dllSaveOtherLosses
	'dim sSaveOtherLosses
		
	'set dllSaveOtherLosses = Server.CreateObject("KeyTax.clsLoadIRP3C")
			
	'i = 0
			
	'for i = i to ubound(ArrLossOther)
				
		'if ArrLossOther(i) <> "" AND ArrLossNature(i) <> "" and sErrorMessage = "" then
						
			'sSaveOtherLosses =  dllSaveOtherLosses.SaveOtherLosses(nReqNo, ArrLossOther(i), ArrLossNature(i))
					
			'if sSaveOtherLosses <> "Saved" then
				'SaveOtherLosses = ArrLossNature(i) & " could not be saved"
			'end if
					
		'end if
			
	'next
			
	'clean up
	'set dllSaveOtherLosses = nothing
	
	dim dllSaveOtherLosses
	dim sSaveOtherLosses
			
	set dllSaveOtherLosses = Server.CreateObject("KeyTax.clsLoadIRP3C")
			
		if nLossNature <> "" AND nLossAmount <> "" and sErrorMessage = "" then
						
			'Response.Write nReqNo & "," & Cstr(nLossNature)& "," & Cstr(nLossAmount)
			sSaveOtherLosses =  dllSaveOtherLosses.SaveOtherLosses(nReqNo, nLossNature, nLossAmount)
					
			if sSaveOtherLosses <> "Saved" then
				SaveOtherLosses = nLossNature & " could not be saved"
			end if
					
		end if

	'clean up
	set dllSaveOtherLosses = nothing
	
End Function
'---------------------------------------------------------------------------------------------------------------------------
%>
